﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.UserControls;

    public class LotAttributeCodeList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F2272;
        protected GridView gvLotAttributeCode;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblLotAttributeCode;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2LotAttributeCodeList(LotAttributeCodeQueryEntity lotAttributeCodeQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(lotAttributeCodeQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoDetailServiceClientProxy.GetInstance().GetDetaiInfolByQueryEntity(lotAttributeCodeQuery, "LOT_ATTRIBUTE_CODE");
            base.BindData2GridView(this.gvLotAttributeCode, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> groupID = base.GetSelectedIDsInGridView(this.gvLotAttributeCode, "GROUP_ID");
                List<string> codeID = base.GetSelectedIDsInGridView(this.gvLotAttributeCode, "CODE_ID");
                if ((groupID == null) || (groupID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(groupID, codeID, new List<string>(), "LOT_ATTRIBUTE_CODE") + base.GetGlobalMsgResource("C006");
                    LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
                    this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> groupID = base.GetSelectedIDsInGridView(this.gvLotAttributeCode, "GROUP_ID");
            List<string> codeID = base.GetSelectedIDsInGridView(this.gvLotAttributeCode, "CODE_ID");
            if ((groupID == null) || (groupID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (groupID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("LotAttributeCode.aspx?from=list&ID={0}&ID2={1}", base.UrlEncode(groupID[0]), base.UrlEncode(codeID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
            lotAttributeCodeQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = lotAttributeCodeQuery.CurrentPage;
            lotAttributeCodeQuery.CurrentPage = "0";
            this.gvLotAttributeCode.Columns[1].Visible = false;
            this.gvLotAttributeCode.AllowSorting = false;
            this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
            this.hdnActionID.Value = "";
            this.gvLotAttributeCode.ShowFooter = false;
            lotAttributeCodeQuery.IsGetAll = false;
            lotAttributeCodeQuery.CurrentPage = currentPageIndex;
            this.SetLotAttributeCodeQueryEntity(lotAttributeCodeQuery);
            base.Export2Excel("LotAttributeCode.xls", this.gvLotAttributeCode);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
            lotAttributeCodeQuery.CurrentPage = "0";
            this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
            lotAttributeCodeQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
            lotAttributeCodeQuery.CodeID = "";
            lotAttributeCodeQuery.GroupID = "";
            lotAttributeCodeQuery.IsActive = "";
            this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
            if (int.Parse(lotAttributeCodeQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                lotAttributeCodeQuery.CurrentPage = (int.Parse(lotAttributeCodeQuery.CurrentPage) + 1).ToString();
                this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
            if (int.Parse(lotAttributeCodeQuery.CurrentPage) > 0)
            {
                lotAttributeCodeQuery.CurrentPage = (int.Parse(lotAttributeCodeQuery.CurrentPage) - 1).ToString();
                this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
            }
        }

        private LotAttributeCodeQueryEntity GetLotAttributeCodeQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as LotAttributeCodeQueryEntity);
            }
            return (base.PageQueryEntitySession as LotAttributeCodeQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LotAttributeCodeQueryEntity lotAttributeCodeQuery = this.GetLotAttributeCodeQueryEntity();
                lotAttributeCodeQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
            }
        }

        protected void gvLotAttributeCode_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F2272", this.gvLotAttributeCode);
        }

        protected void gvLotAttributeCode_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvLotAttributeCode_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LotAttributeCodeQueryEntity lotAttribute = this.GetLotAttributeCodeQueryEntity();
                lotAttribute.SortField = e.SortExpression;
                if (lotAttribute.SortDirection == "ASC")
                {
                    lotAttribute.SortDirection = "DESC";
                }
                else if (lotAttribute.SortDirection == "DESC")
                {
                    lotAttribute.SortDirection = "ASC";
                }
                this.BindData2LotAttributeCodeList(lotAttribute);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            LotAttributeCodeQueryEntity lotAttributeCodeQuery = new LotAttributeCodeQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                lotAttributeCodeQuery.CodeID = this.PreviousPage.LotAttributeCodeID;
                lotAttributeCodeQuery.GroupID = this.PreviousPage.LotAttributeCodeGroup;
                lotAttributeCodeQuery.IsActive = this.PreviousPage.IsActive;
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            lotAttributeCodeQuery.IsGetAll = false;
            lotAttributeCodeQuery.CurrentPage = "0";
            lotAttributeCodeQuery.PageSize = this.gvLotAttributeCode.PageSize.ToString();
            lotAttributeCodeQuery.SortField = "GROUP_ID ASC, SEQ_NO";
            lotAttributeCodeQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvLotAttributeCode.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetLotAttributeCodeQueryEntity(lotAttributeCodeQuery);
            this.BindData2LotAttributeCodeList(lotAttributeCodeQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetLotAttributeCodeQueryEntity(LotAttributeCodeQueryEntity lotAttributeCodeQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = lotAttributeCodeQuery;
            }
            else
            {
                base.PageQueryEntitySession = lotAttributeCodeQuery;
            }
        }

        public LotAttributeCodeQuery PreviousPage
        {
            get
            {
                return (LotAttributeCodeQuery) base.PreviousPage;
            }
        }
    }
}

